<?php

namespace app\beautiful\controller\miniapp\staff\appointment;

use app\BaseController;

use app\beautiful\model\appointment\PerformanceItemModel;

use app\beautiful\model\appointment\PerformanceModel;

use app\beautiful\model\appointment\PerformanceTypeModel;

use think\facade\Db;



class Performance extends BaseController{

    protected function initialize()

    {

        if (!in_array(14, $this->request->staff->add_value_jurisdiction)) {

            $this->error('您没有业绩填单的权限')->send();

            die();

        }

    }



    public function getPerformanceList()

    {

        $type = $this->request->param('type/d');

        $where = [

            ['shop_id','=',$this->request->shopId],

            ['staff_id','=',$this->request->staffId],

        ];



        switch ($type){ //查询订单的状态

            case 0: // 待核准

                $where[] = ['status','=',1];

                break;

            case 1://核准通过

                $where[] = ['status','=',8];

                break;

            case 2://核准不过

                $where[] = ['status','=',-1];

                break;

        }



        $datas=PerformanceModel::with(['items'])->where($where)

            ->order(['add_time'=>'desc'])

            ->field('performance_id,commission,pictures,mobile,remark,status,add_time')

            ->limit($this->request->limit)

            ->page($this->request->page)

            ->select()

            ->append(['add_time_format'])

            ->toArray();



        $return['datas']=$datas;

        return $this->success($return);

    }



    public function getPerformanceTypeList()

    {

        $where = [

            ['shop_id','=',$this->request->shopId],

            ['is_delete','=',0],

        ];

        $datas=PerformanceTypeModel::where($where)->field('performance_type_id,name')

            ->select()

            ->toArray();

        if(!empty($datas)){

            foreach ($datas as $k => $v){

                $datas[$k]['num']=1;

                $datas[$k]['select']=0;

            }

        }

        $return['datas']=$datas;

        return $this->success($return);

    }



    public function addPerformance()

    {

        $performance=$this->request->param('performance/a',[]);

        $pictures=$this->request->param('pictures/a',[]);

        $mobile=$this->request->param('mobile');

        $remark=$this->request->param('remark');



        if(empty($performance) || !is_array($performance)) return $this->error('请选择填单类型');

        $performance_type_ids=[];$performance_arr=[];

        foreach ($performance as $k => $v){

            if(!isset($v['performance_type_id'])) return $this->error('参数错误');

            if($v['performance_type_id']<=0) return $this->error('参数错误');

            if(!isset($v['num'])) return $this->error('参数错误');

            if($v['num']<=0) return $this->error('数量不能小于0');

            $performance_type_ids[]=$v['performance_type_id'];$performance_arr[$v['performance_type_id']]=$v;

        }

        if(empty($pictures) || !isset($pictures)) return $this->error('请上传图片');

        if(count($pictures)<=0 || count($pictures)>3) return $this->error('图片数量三张之内');

        if(empty($mobile) || !is_mobile($mobile)) return $this->error('请上传手机号');

        if(empty($remark)) return $this->error('请输入备注');



        $performance_type=PerformanceTypeModel::where([

            ['shop_id','=',$this->request->shopId],

            ['performance_type_id','in',$performance_type_ids],

            ['is_delete','=',0],

        ])->field('performance_type_id,name,single_commission')->select()->toArray();

        if(empty($performance_type)) return $this->error('请选择业绩类型');



        Db::startTrans();

        try{

            $performance_model=new PerformanceModel();

            $performance_model->shop_id=$this->request->shopId;

            $performance_model->staff_id=$this->request->staffId;

            $performance_model->pictures=$pictures;

            $performance_model->mobile=$mobile;

            $performance_model->remark=$remark;

            $performance_model->save();



            $total_commission=0;$performance_items_data=[];

            foreach ($performance_type as $k => $v){

                $num=$performance_arr[$v['performance_type_id']]['num'];

                $commission=round(($v['single_commission']*100*$num)/100,2);

                $performance_items_data[]=[

                    'shop_id'=>$this->request->shopId,

                    'staff_id'=>$this->request->staffId,

                    'performance_id'=>$performance_model->performance_id,

                    'performance_type_id'=>$v['performance_type_id'],

                    'type_name'=>$v['name'],

                    'single_commission'=>$v['single_commission'],

                    'num'=>$performance_arr[$v['performance_type_id']]['num'],

                    'commission'=>$commission

                ];

                $total_commission=round(($total_commission*100 + $commission*100)/100,2);

            }



            $performance_model->commission=$total_commission;

            $performance_model->save();



            $performance_item_model= new PerformanceItemModel();

            $performance_item_model->saveAll($performance_items_data);

            Db::commit();

            return $this->success('添加成功');

        }catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }

}

